package com.irdstudio.tdp.executor.core.plugin.migrate;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.irdstudio.tdp.executor.core.plugin.AbstractPlugin;
import com.irdstudio.tdp.executor.core.plugin.migrate.dao.MigrateFieldMapping;
import com.irdstudio.tdp.executor.core.plugin.migrate.dao.MigrateFieldMappingDao;
import com.irdstudio.tdp.executor.core.plugin.migrate.dao.MigrateTableMapping;
import com.irdstudio.tdp.executor.core.plugin.migrate.dao.MigrateTableMappingDao;

/**
 * 
 * 迁移表数据插件:
 * 1、源到到目标表迁移，根据配置进行迁移
 * 2、SQL语句到目标表迁移，根据配置进行迁移
 * @author ligm
 *
 */
public class MigrateTableDataPlugin extends AbstractPlugin {
	
	/** 表数据迁移映射配置 */
	private MigrateTableMapping tableMapping = null;

	/** 表字段映射配置信息 */
	private List<MigrateFieldMapping> fieldMappingList = null;

	
	@Override
	protected boolean doReadConfigureFromDB(Connection conn, String szConfIdentify) throws SQLException {
		//获取插件配置数据示例写法，如下：		
		// 根据表映射标识获取配置信息
		String tableMappingid = szConfIdentify;
		MigrateTableMappingDao tableDao = new MigrateTableMappingDao(conn);
		String szCondition=" where table_mapping_id='"+tableMappingid+"'";
		tableMapping = tableDao.queryMigrateTableMappingWithCond(szCondition, "").get(0);

		//根据表映射标识获取字段映射配置
		MigrateFieldMappingDao fieldDao = new MigrateFieldMappingDao(conn);
		String fieldszCondition=" where table_mapping_id='"+tableMappingid+"'";
		fieldMappingList = fieldDao.queryMigrateFieldMappingWithCond(fieldszCondition,"");

		return true;
	}
	
	@Override
	public boolean execute() {
		// 在此完成具体表数据迁移插件的逻辑
		migrateTableDataUtil  migrateTableDataUtil=new migrateTableDataUtil();
		//表数据迁移执行逻辑方法
		boolean reulst=migrateTableDataUtil.datamigration(tableMapping,fieldMappingList,this.getPluginConnection());
		return reulst;
	}
}
